import os,cv2
from tools import  toolsClass

import random


def mkPath(path):
    if(os.path.exists(path)):
        return path
    os.mkdir(path)
    return path


def getDetail(imgPath,labelPath,all,itname):

    saveAll = open(all,'w')

    totalTime = 0
    for i in range(100):
        totalTime += toolsClass.getTime()

    num = 1
    for i,file in enumerate(os.listdir(imgPath)):
        print(i)
        # for file in sorted(os.listdir(imgPath),key=lambda b:int(b.split(".")[0].split("_")[-1])):
        if(file.endswith("g")):
            imgName = file.split(".")[0]
            print(imgName)
            img = cv2.imread(os.path.join(imgPath,imgName+ "."+file.split(".")[-1]))

            _,iw,ih = img.shape[::-1]

            # time = file.split("&")[-1].split(".")[0]
            # filePath = os.path.join(imgPath,file)
            txtPath = os.path.join(labelPath,imgName+".txt")

            # if(not os.path.exists(txtPath)):
            #     continue

            with open(txtPath,"r") as f:
                lines = f.readlines()
                useTime = toolsClass.getTime()
                save = False
                for line in lines:
                    line = line.rstrip()
                    carID,rx,ry,rw,rh = line.split(" ")
                    label = carID
                    # if(label!="1"):
                    #     continue
                    print("num=",num)
                    num+=1
                    save = True
                    # label = int(label)+100
                    label = classID[itname]
                    rx, ry, rw, rh = map(float,[rx,ry,rw,rh])
                    x,y,w,h = toolsClass.convertR(rx, ry, rw, rh,iw,ih)
                    if(w<40 and h<40):
                        if w<40:
                            w=random.randint(40,43)
                        if h<40:
                            h=random.randint(40,43)

                        if w>500 and  h>500:
                            print("-------------------------------------",imgName)

                    # msg = [imgName+".jpg",w,h,time]
                    # msg = " ".join(map(str,[imgName+".jpg","classes"+label,w,h,useTime]))


                    msg = imgName+".jpg"+", classes:"+str(label)+", w:"+str(w)+", h:"+str(h)+", size:"+str(w*h)+", time:"+str(useTime)
                    saveAll.write(msg+"\n")

                    cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
                    # cv2.putText(img,str(label)+",w:"+str(w)+",h:"+str(h)+", size:"+str(w*h)+",time:"+str(useTime),(x,y),2,1,(0,0,255),2)
                    cv2.putText(img,str(carID),(x,y),2,1,(0,0,255),2)

                if save:
                # if (False):

                    img = cv2.resize(img,(960,540))
                    cv2.putText(img, "Total Time: " + str(totalTime) + "ms", (10, 60), 2, 2, (255, 0, 255), 2)
                    cv2.putText(img, "This Image: " + str(useTime) + "ms", (10, 130), 2, 2,(255, 0, 255), 2)

                    cv2.putText(img, "Car ID: " + str(carID) , (10, 200), 2, 2,(255, 0, 255), 2)


                    cv2.imshow("imh",img)
                    resultPath = mkPath(all.split(".")[0]+"_result")

                    cv2.imwrite(os.path.join(resultPath,imgName+".jpg"),img)
                    cv2.waitKey(1)

classID = {"0_河道漂浮物":0,
           "1_人脸属性":1,
           "2_河岸排污口":2,
           "3_人群密度":3,
           "4_河岸违章建筑":4,
           "5_水面游泳":5,
           "6_翻墙":6,
           "7_废弃排放":7,
           "8_工程车":8,
           "9_船只":9,
           "10_烟雾":10,
           "11_车牌":11,
           "12_垃圾":12,
           "13_动物":13,
           "14_人体":14,
           "15_抽烟":15,#3
           "16_打电话":16,#2
           "17_打架":17,#1
           "18_倒地":18
           }


def main():
    # imgPath = "/mnt/data/song/dataset/animals/"
    # labelPath = "/home/tsl/labels/animals"

    # imgPath = "/mnt/data/song/dataset/people/"
    # labelPath = "/home/tsl/labels/people"

    itemName = "17_打架"
    # itemName = str(classID[itemName])+"_"+itemName

    keys = list(classID.keys())
    for k in keys:
        print(k)
        if k != "11_车牌":
            continue
        itemName = k
        imgPath = "/mnt/data/jjn/dataset/"+itemName
        labelPath = "/mnt/data/jjn/labels/"+itemName

        all = os.path.join("/mnt/data/jjn/msg", imgPath.split("/")[-1] + ".txt")

        getDetail(imgPath,labelPath,all,itemName)

if __name__ == "__main__":
    main()